package com.CST.service.impl;

import com.CST.dto.RubbingDTO;
import com.CST.entity.BdEnumdetail;
import com.CST.mapper.cst.InspectionCheckMapper;
import com.CST.mapper.dataplatform.BdEnumdetailMapper;
import com.CST.service.InspectionCheckService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author qtx
 * @since 2022/9/21
 */
@Service
public class InspectionCheckServiceImpl implements InspectionCheckService {

    @Autowired
    private InspectionCheckMapper inspectionCheckMapper;

    @Autowired
    private BdEnumdetailMapper mapper;

    private List<BdEnumdetail> getBdList(String s) {
        assert s != null;
        return mapper.selectList(Wrappers.lambdaQuery(BdEnumdetail.class).eq(BdEnumdetail::getEdParentCode, s));
    }

    @Override
    public List<String> rubbing1(RubbingDTO dto) {
        Set<String> set = getBdList("RD_DeptSalaryRelation").stream()
                .map(BdEnumdetail::getEdExtraFirstCode)
                .collect(Collectors.toSet());
        List<String> list = inspectionCheckMapper.selectByTime1(dto);
        return getStrings(set, list, "薪酬科室");
    }

    @Override
    public List<String> rubbing2(RubbingDTO dto) {
        Set<String> set = getBdList("RD_DeptHisRelation").stream()
                .map(BdEnumdetail::getEdExtraFirstCode)
                .collect(Collectors.toSet());
        List<String> select = inspectionCheckMapper.selectByTime21(dto);
        List<String> select1 = inspectionCheckMapper.selectByTime22(dto);
        List<String> strings = new ArrayList<>();
        strings.addAll(getStrings(set, select, "HIS科室,住院科室编号"));
        strings.addAll(getStrings(set, select1, "HIS科室,门诊科室编号"));
        return strings;
    }

    @Override
    public List<String> rubbing3(RubbingDTO dto) {
        setTime(dto);
        Set<String> set = getBdList("RD_DeptUFIDARelation").stream()
                .map(BdEnumdetail::getEdExtraFirstCode)
                .collect(Collectors.toSet());
        List<String> list = inspectionCheckMapper.selectByTime3(dto);
        return getStrings(set, list, "用友科室");
    }

    @Override
    public List<String> rubbing4(RubbingDTO dto) {
        setTime(dto);
        Set<String> set = getBdList("RD_CostUFIDARelation").stream()
                .map(BdEnumdetail::getEdExtraFirstCode)
                .collect(Collectors.toSet());
        List<String> list = inspectionCheckMapper.selectByTime4(dto);
        return getStrings(set, list, "成本会计科目");
    }

    @Override
    public List<String> rubbing5(RubbingDTO dto) {
        setTime(dto);
        Set<String> set = getBdList("RD_IncomeUFIDARelation").stream()
                .map(BdEnumdetail::getEdExtraFirstCode)
                .collect(Collectors.toSet());
        List<String> list = inspectionCheckMapper.selectByTime5(dto);
        return getStrings(set, list, "收入会计科目");
    }

    @Override
    public List<String> rubbing6(RubbingDTO dto) {
        Set<String> set = new HashSet<>(inspectionCheckMapper.selectByTime61(dto));
        List<String> list = inspectionCheckMapper.selectByTime6(dto);
        return getStrings(set, list, "薪酬");
    }

    private List<String> getStrings(Set<String> set, List<String> list, String ss) {
        List<String> vo = new ArrayList<>();
        list.forEach(e -> {
            if (!set.contains(e)) {
                if (StringUtils.isNotBlank(e)) {
                    vo.add(ss + e + "未对应");
                }
            }
        });
        return vo;
    }

    public void setTime(RubbingDTO dto) {
        StringBuilder builder = new StringBuilder();
        builder.append(dto.getYear());
        if (dto.getMonth() < 10) {
            builder.append(0).append(dto.getMonth());
        } else {
            builder.append(dto.getMonth());
        }
        dto.setTime(builder.toString());
    }
}
